{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "global-street",
   "metadata": {},
   "source": [
    "# Run simulations with O'Regan 2022 parameter set (LG M50)\n",
    "\n",
    "In this notebook we show an example on how to run the DFN model with the O'Regan 2022 parameter set for the LG M50 cell. Because of the concentration dependent diffusion coefficient, we need to customise the mesh so the simulations converge."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "unavailable-communication",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install \"pybamm[plot,cite]\" -q    # install PyBaMM if it is not installed\n",
    "import pybamm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prompt-scottish",
   "metadata": {},
   "source": [
    "We first initialise the model and define the parameter set and the solver:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "domestic-naples",
   "metadata": {},
   "outputs": [],
   "source": [
    "# DFN + lumped thermal\n",
    "options = {\"thermal\": \"lumped\", \"dimensionality\": 0, \"cell geometry\": \"arbitrary\"}\n",
    "model = pybamm.lithium_ion.DFN(options=options)\n",
    "\n",
    "# O'Regan 2022 parameter set\n",
    "param = pybamm.ParameterValues(\"ORegan2022\")\n",
    "\n",
    "# Choose CasADI fast (we do a short discharge so there are no events, if events are needed choose \"fast with events\")\n",
    "solver = pybamm.IDAKLUSolver()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "occasional-skill",
   "metadata": {},
   "source": [
    "Now we define the new mesh. Because the particle diffusivity depends on concentration and get drop very abruptly, we set a fine mesh (40 points in each particle). Additionally, given that the larger gradients occur towards the particle surface, we choose an exponential mesh weighted towards the `right` (i.e. the particle surface). This generates a non-uniform mesh with a finer grid towards the particle surface and a coarser grid towards the particle centre."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "vocal-action",
   "metadata": {},
   "outputs": [],
   "source": [
    "var_pts = {\"x_n\": 30, \"x_s\": 30, \"x_p\": 30, \"r_n\": 40, \"r_p\": 40}\n",
    "\n",
    "submesh_types = model.default_submesh_types\n",
    "submesh_types[\"negative particle\"] = pybamm.MeshGenerator(\n",
    "    pybamm.Exponential1DSubMesh, submesh_params={\"side\": \"right\"}\n",
    ")\n",
    "submesh_types[\"positive particle\"] = pybamm.MeshGenerator(\n",
    "    pybamm.Exponential1DSubMesh, submesh_params={\"side\": \"right\"}\n",
    ")\n",
    "\n",
    "# Define the simulation\n",
    "sim = pybamm.Simulation(\n",
    "    model,\n",
    "    parameter_values=param,\n",
    "    C_rate=1,\n",
    "    solver=solver,\n",
    "    var_pts=var_pts,\n",
    "    submesh_types=submesh_types,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "equipped-agenda",
   "metadata": {},
   "source": [
    "Finally, we can solve the simulation and plot the results. Note that because the nonlinear diffusion and the fine mesh, if we want to solve for longer time, such as full discharge or other experiments, the simulations might take a few minutes to run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "shaped-membrane",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2c6b050e722f45c9921d891d4a3c1c19",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.0, description='t', max=1800.0, step=18.0), Output()), _dom_classes=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<pybamm.plotting.quick_plot.QuickPlot at 0x17e0440d0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim = pybamm.Simulation(\n",
    "    model,\n",
    "    parameter_values=param,\n",
    "    C_rate=1,\n",
    "    solver=solver,\n",
    "    var_pts=var_pts,\n",
    "    submesh_types=submesh_types,\n",
    ")\n",
    "sim.solve(\n",
    "    [0, 1800]\n",
    ")  # solving time kept short for testing purposes, feel free to extend it\n",
    "sim.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e86b053e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a79ea249a7c94d43a219067c00259084",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.0, description='t', max=3316.4079454263174, step=33.164079454263174)…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<pybamm.plotting.quick_plot.QuickPlot at 0x29e78fd50>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim = pybamm.Simulation(\n",
    "    model,\n",
    "    parameter_values=param,\n",
    "    C_rate=1,\n",
    "    solver=solver,\n",
    "    # var_pts=var_pts,\n",
    "    # submesh_types=submesh_types,\n",
    ")\n",
    "sim.solve(\n",
    "    [0, 3600]\n",
    ")  # solving time kept short for testing purposes, feel free to extend it\n",
    "sim.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "lined-illustration",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.\n",
      "[2] Von DAG Bruggeman. Berechnung verschiedener physikalischer konstanten von heterogenen substanzen. i. dielektrizitätskonstanten und leitfähigkeiten der mischkörper aus isotropen substanzen. Annalen der physik, 416(7):636–664, 1935.\n",
      "[3] Chang-Hui Chen, Ferran Brosa Planella, Kieran O'Regan, Dominika Gastol, W. Dhammika Widanage, and Emma Kendrick. Development of Experimental Techniques for Parameterization of Multi-scale Lithium-ion Battery Models. Journal of The Electrochemical Society, 167(8):080534, 2020. doi:10.1149/1945-7111/ab9050.\n",
      "[4] Marc Doyle, Thomas F. Fuller, and John Newman. Modeling of galvanostatic charge and discharge of the lithium/polymer/insertion cell. Journal of the Electrochemical society, 140(6):1526–1533, 1993. doi:10.1149/1.2221597.\n",
      "[5] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.\n",
      "[6] Alan C. Hindmarsh. The PVODE and IDA algorithms. Technical Report, Lawrence Livermore National Lab., CA (US), 2000. doi:10.2172/802599.\n",
      "[7] Alan C. Hindmarsh, Peter N. Brown, Keith E. Grant, Steven L. Lee, Radu Serban, Dan E. Shumaker, and Carol S. Woodward. SUNDIALS: Suite of nonlinear and differential/algebraic equation solvers. ACM Transactions on Mathematical Software (TOMS), 31(3):363–396, 2005. doi:10.1145/1089014.1089020.\n",
      "[8] Kieran O'Regan, Ferran Brosa Planella, W. Dhammika Widanage, and Emma Kendrick. Thermal-electrochemical parameters of a high energy lithium-ion cylindrical battery. Electrochimica Acta, 425:140700, 2022. doi:10.1016/j.electacta.2022.140700.\n",
      "[9] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.\n",
      "[10] Robert Timms, Scott G Marquis, Valentin Sulzer, Colin P. Please, and S Jonathan Chapman. Asymptotic Reduction of a Lithium-ion Pouch Cell Model. SIAM Journal on Applied Mathematics, 81(3):765–788, 2021. doi:10.1137/20M1336898.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "pybamm.print_citations()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "vscode": {
   "interpreter": {
    "hash": "bca2b99bfac80e18288b793d52fa0653ab9b5fe5d22e7b211c44eb982a41c00c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
